package com.yiwan.app.preventionsis.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.app.tianwan.tianwanframe.utils.LogUtil;
import com.app.tianwan.tianwanframe.utils.SystemTool;
import com.yiwan.app.preventionsis.R;
import com.yiwan.app.preventionsis.bean.ImageRes;
import com.yiwan.app.preventionsis.config.GlobalCfg;
import com.yiwan.app.preventionsis.util.StorageUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class FragmentShow extends Fragment {

    private static final int SHOW_TYPE_IMG = 0;
    private static final String IMAGE_DATA_EXTRA1 = "imgRes";
    private static final String IMAGE_DATA_EXTRA2 = "imgType";
    private View layout = null;
    private ImageRes imgRes;
    private int imgType;
    private ImageView showImg = null;
    private LinearLayout showAnimLinearlo[] = null;
    private View showAnimViewlo[] = null;
    private static Activity activity;

    private static BitmapDrawable drawableTmp;

    public static FragmentShow newInstance(Activity act, ImageRes imgRes, int imgType) {
        final FragmentShow fShow = new FragmentShow();
        final Bundle args = new Bundle();
        activity = act;
        args.putSerializable(IMAGE_DATA_EXTRA1, imgRes);
        args.putInt(IMAGE_DATA_EXTRA2, imgType);
        fShow.setArguments(args);
        return fShow;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imgRes = (ImageRes)getArguments().getSerializable(IMAGE_DATA_EXTRA1);
        imgType = getArguments().getInt(IMAGE_DATA_EXTRA2);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(drawableTmp != null && drawableTmp.getBitmap() != null){
            drawableTmp.getBitmap().recycle();
            drawableTmp = null;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.fragmemt_show, null);
        showImg = (ImageView) layout.findViewById(R.id.show_img);
        showAnimLinearlo = new LinearLayout[2];
        showAnimLinearlo[0] = (LinearLayout) layout.findViewById(R.id.show_lo_top);
        showAnimLinearlo[1] = (LinearLayout) layout.findViewById(R.id.show_lo_bottom);
        showAnimViewlo = new View[6];
        showAnimViewlo[0] = layout.findViewById(R.id.show_lo_top_l);
        showAnimViewlo[1] = layout.findViewById(R.id.show_lo_top_c);
        showAnimViewlo[2] = layout.findViewById(R.id.show_lo_top_r);
        showAnimViewlo[3] = layout.findViewById(R.id.show_lo_bottom_l);
        showAnimViewlo[4] = layout.findViewById(R.id.show_lo_bottom_c);
        showAnimViewlo[5] = layout.findViewById(R.id.show_lo_bottom_r);
        String imgFolder = GlobalCfg.RES_FOLDER + File.separator + GlobalCfg.FOLDERS[imgType] + File.separator;
        if (SystemTool.isArrayNullOrEmpty(imgRes.getAnimImgs())) {
            LogUtil.d("show img: " + imgFolder + imgRes.getImgPath());
            drawableTmp = new BitmapDrawable(getResources(), readBitMap(activity, imgFolder + imgRes.getImgPath()));
            showImg.setImageDrawable(drawableTmp);
            drawableTmp = null;
            showAnimLinearlo[0].setVisibility(View.GONE);
            showAnimLinearlo[1].setVisibility(View.GONE);
        } else {
            showAnimLinearlo[0].setVisibility(View.VISIBLE);
            showAnimLinearlo[1].setVisibility(View.VISIBLE);
            showAnimLinearlo[0].setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, 0, Float.parseFloat(imgRes.getAnimLayout().get(1))));
            showAnimLinearlo[1].setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, 0, Float.parseFloat(imgRes.getAnimLayout().get(2))));
            for (int i = 0; i < showAnimViewlo.length; i++) {
                showAnimViewlo[i].setLayoutParams(new LinearLayout.LayoutParams(
                        0, LinearLayout.LayoutParams.MATCH_PARENT, Float.parseFloat(imgRes.getAnimLayout().get(i + 3))));
            }
            LogUtil.d("show img: " + imgFolder + imgRes.getImgPath());
            drawableTmp = new BitmapDrawable(getResources(), readBitMap(activity, imgFolder + imgRes.getImgPath()));
            showImg.setImageDrawable(drawableTmp);
            drawableTmp = null;
            AnimationDrawable animationDrawable = new AnimationDrawable();
//                    (AnimationDrawable) showAnimViewlo[animLoArray[0]].getBackground();
            for(int i = 0; i < imgRes.getAnimImgs().size(); i++){
                String animPath = StorageUtil.getAppFilePath().getAbsolutePath() + File.separator + imgFolder + imgRes.getAnimImgs().get(i);
                LogUtil.d("show imgAnim: " + animPath);
                animationDrawable.addFrame(Drawable.createFromPath(animPath), Integer.parseInt(imgRes.getAnimDuration().get(i)));
            }
            showAnimViewlo[Integer.parseInt(imgRes.getAnimLayout().get(0))].setBackgroundDrawable(animationDrawable);
            animationDrawable.setOneShot(false);
            animationDrawable.start();
        }

        return layout;
    }

    public static Bitmap readBitMap(Context context, String path) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;    //��Ϊtrue��ʾ��ϵͳҪ�ڴ��ʱ�򣬿��Ի�����
        //���ͼƬ������Ҫ���Ա����յĻ������opt.inInputShareable=true;������bitmap�������˵����
        //This field works in conjuction with inPurgeable. If inPurgeable is false,
        //then this field is ignored. If inPurgeable is true,
        //then this field determines whether the bitmap can share run reference to the
        //input data (inputstream, array, etc.) or if it must make run deep copy.
        opt.inInputShareable = true;
        //��ȡ��ԴͼƬ
        try{
            String imgPath = StorageUtil.getAppFilePath().getAbsolutePath() + File.separator + path;
            FileInputStream is = new FileInputStream(imgPath);
            Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt);
//            LogUtil.d("img: " + imgPath + ", bitmap = " + bitmap);
            return bitmap;
        } catch (FileNotFoundException e){
            return null;
        }
    }
}